﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;
using System.Data;
//

namespace PerceptiveMCAPI_Test
{
   public partial class show_listBatch : System.Web.UI.UserControl
   {
      public void Display( listBatchUnsubscribeOutput output )
      {
         this.Visible = true;

         lblCounts.Text = string.Format( "Success count: {0}, Error count: {1}",
            output.result.success_count, output.result.error_count );
         if ( output.result.errors.Count > 0 )
         {
            // manual format
            DataTable errTable = FormatUnsubscribeErrors( output.result.errors );
            gvBatchErrors.DataSource = errTable;
            gvBatchErrors.DataBind();
         }
         else
         {
            gvBatchErrors.DataSource = "";
            gvBatchErrors.DataBind();
         }
      }
      // --
      public void Display( listBatchSubscribeOutput output )
      {
         this.Visible = true;

         lblCounts.Text = string.Format( "Success count: {0}, Error count: {1}",
            output.result.success_count, output.result.error_count );

         if ( output.result.errors.Count > 0 )
         {
            // manual format to get row values to display
            DataTable errTable = FormatSubscribeErrors( output.result.errors );
            gvBatchErrors.DataSource = errTable;
            gvBatchErrors.DataBind();
         }
         else
         {
            gvBatchErrors.DataSource = "";
            gvBatchErrors.DataBind();
         }
      }

      private DataTable FormatSubscribeErrors( List<listBatchSubscribeError> errors )
      {
         DataTable tbl = new DataTable( "BatchErrors" );
         tbl.Columns.Add( "Code" ); tbl.Columns.Add( "Message" ); tbl.Columns.Add( "RowValues" );
         foreach ( listBatchSubscribeError error in errors )
         {
            DataRow row = tbl.NewRow();
            row["Code"] = error.code.ToString();
            row["Message"] = error.message;
            row["RowValues"] = ApiHelper.DictionaryToStringHtmlStack( error.row );
            tbl.Rows.Add( row );
         }
         return tbl;
      }
      // ---------------------------------------
      private DataTable FormatUnsubscribeErrors( List<listBatchUnsubscribeError> errors )
      {
         DataTable tbl = new DataTable( "BatchErrors" );
         tbl.Columns.Add( "Code" ); tbl.Columns.Add( "Message" ); tbl.Columns.Add( "RowValues" );
         foreach ( listBatchUnsubscribeError error in errors )
         {
            DataRow row = tbl.NewRow();
            row["Code"] = error.code.ToString();
            row["Message"] = error.message;
            row["RowValues"] = error.email;
            tbl.Rows.Add( row );
         }
         return tbl;
      }

   }
}